package com.leisure.services.impl;

import com.leisure.mappers.UserMapper;
import com.leisure.pojo.TUser;
import com.leisure.services.UserService;
import com.leisure.utils.MD5Util;
import com.leisure.utils.RSAUtil;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xmlunit.util.Mapper;

import java.time.LocalDateTime;

/**
 * ClassName: UserService
 * Package: com.leisure.service.impl
 * Description:
 *
 * @Author: MyLeisureLife
 * @Date: 2024/6/5:01:05:25 星期三
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    public UserServiceImpl(UserMapper userMapper){
        this.userMapper = userMapper;
    }

    UserMapper userMapper;

    @Override
    public TUser findByUserName(String md5Username) {
        // 查找加密的用户名
        return userMapper.findByUserName(md5Username);
    }

    @Override
    public void register(String encryptUsername, String encryptPassword) {
        // RSA解密获取用户名
        @Pattern(regexp = "^\\S{5,16}$") String username = RSAUtil.decryptByPrivateKey(encryptUsername);
        // RSA解密获取米啊
        @Pattern(regexp = "^\\S{5,16}$") String password = RSAUtil.decryptByPrivateKey(encryptPassword);
        //加密用户名
        String md5Username = MD5Util.getMD5String(username);
        //加密密码
        String md5Password = MD5Util.getMD5String(password);
        //注册
        userMapper.add(md5Username, md5Password);

    }

    /**
     * 更新用户
     * @param user 用户数据
     */
    @Override
    public void update(TUser user) {
        // 设置更新时间
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }
}
